import sun.reflect.generics.tree.Tree;

import javax.swing.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //public TreeNode root;//根节点

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    public void preOrder(TreeNode root) {
        if (root == null) return ;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //遍历子路 前序遍历  标注

    /**
     * 难点 便利思路 和子问题思路
     * @param root
     * @return
     */
    /*List<Character> ret = new ArrayList<>();
    public List<Character> preorderTraversal(TreeNode root) {
        if (root == null) return ret;
        ret.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return ret;
    }*/
    /*public List<Character> preorderTraversal(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if (root == null) return ret;//返回0
        ret.add(root.val);

        List<Character> leftTree = preorderTraversal(root.left);
        //这一步把之前递归的值放入了当前的list存储 然后在一段一段的被销毁 确保了第一个list能接受到所有的值
        ret.addAll(leftTree);

        List<Character> rightTree = preorderTraversal(root.right);
        ret.addAll(rightTree);

        return ret;
    }*/

    public List<Character> preorderTraversal(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if (root == null) return ret;
        TreeNode cur = root;
        ret.add(cur.val);
        List<Character> leftH = preorderTraversal(cur.left);
        ret.addAll(leftH);
        List<Character> rightH = preorderTraversal(cur.right);
        ret.addAll(rightH);
        return ret;

        /*List<Character> ret = new ArrayList<>();
        if (ret == null) return ret;
        ret.add(root.val);//把当前遍历到的值添加到ret

        //开始遍历下一个左节点
        List<Character> TreeLeft = preorderTraversal(root.left);
        //第一次到这一步说明已经遍历完到最后的null节点了
        ret.addAll(TreeLeft);//把之前的全部添加进去

        //遍历右节点
        List<Character> TreeRight = preorderTraversal(root.right);
        ret.addAll(TreeRight);

        return ret;//遍历完毕*/
    }

    //中序遍历
    void inOrder(TreeNode root) {
        if (root == null) return ;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

   /* public List<Character> inOrderTraversal(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if (root == null) return ret;

        List<Character> LeftTree = inOrderTraversal(root.left);
        ret.addAll(LeftTree);

        ret.add(root.val);//添加节点

        List<Character> RightTree = inOrderTraversal(root.right);
        ret.addAll(RightTree);

        return ret;
    }*/

    void postOrder(TreeNode root) {
        if (root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public List<Character> postoderTraversal (TreeNode root){
        List<Character> ret = new ArrayList<>();
        if (root == null) return ret;

        List<Character> leftTree = postoderTraversal(root.left);
        ret.addAll(leftTree);

        List<Character> RightTree = postoderTraversal(root.right);
        ret.addAll(RightTree);

        ret.add(root.val);

        return ret;
    }

    //获取节点的个数 遍历思路
    public static int nodeSize = 0;
    public void size(TreeNode root) {
        if (root == null) return ;
        nodeSize ++;
        size(root.left);
        size(root.right);
    }

    //获取节点的个数 子问题思路
    public int size2 (TreeNode root) {
        if (root == null) return 0;
        return size2(root.left) + size2(root.right) + 1;
    }

    //获取获取叶子节点的个数 ：遍历思路

    public static int leafSize = 0;
    void getLeafNodeCount1(TreeNode root) {
        if (root == null) return ;
        if (root.left == null && root.right == null) {
            leafSize ++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    //获取叶子结点的个数 子问题

    int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) return 1;
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.left);
    }

    //获取第k层节点的个数
    int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) return 0;
        if (k == 1) return 1;
        return getKLevelNodeCount(root.left , k -1) + getKLevelNodeCount(root.right, k-1);
    }


    //获取二叉树的高度
    int getHeight(TreeNode root) {
        if (root == null) return 0;
        int leafH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return leafH > rightH ? leafH + 1 : rightH + 1;
    }

    int getHeight2(TreeNode root) {
        if (root == null) return 0;
        return getHeight2(root.left) > getHeight2(root.right)
                ? getHeight2(root.left) + 1 : getHeight2(root.right) + 1;
    }


    //检测value的值是否存在
    TreeNode find(TreeNode root, char val) {
        if (root == null) return null;
        if (root.val == val) return root;

        TreeNode ret = find(root.left, val) ;
        if (ret != null) return ret;
        ret = find(root.right, val);
        if (ret != null) return ret;

        return null;
    }

    //层序遍历
    void levelOrder(TreeNode root) {
        if (root == null) return ;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) queue.offer(cur.left);
            if (cur.right != null) queue.offer(cur.right);
        }
    }
    //类似二维数组
    public List<List<Character>> levelOlder2(TreeNode root) {
        List<List<Character>> ret = new ArrayList<>();
        if (root == null) return ret;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            List<Character> list = new ArrayList<>();
            int size = queue.size();
            while(size != 0) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                size--;
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
            }
            ret.add(list);
        }
        return ret;
    }


}
